package com.topisv.tms.web.report;

import java.io.PrintWriter;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.topisv.tms.entity.AreaGroup;
import com.topisv.tms.entity.Customers;
import com.topisv.tms.entity.EdiOrder;
import com.topisv.tms.entity.JSONParam;
import com.topisv.tms.entity.dto.InstorageInfoDto;
import com.topisv.tms.entity.stock.instorage.InstorageInfo;
import com.topisv.tms.service.AreaGroupService;
import com.topisv.tms.service.CustomersService;
import com.topisv.tms.service.account.ShiroDbRealm.ShiroUser;
import com.topisv.tms.tools.DataTablesUtils;
import com.topisv.tms.utils.DateUtils;

/**
 * 进存途销—control
 * @author ch
 *
 */
@Controller
@RequestMapping(value = "/v2/jctx")
public class JCTXController {

	@Autowired
	private CustomersService customersService;
	@Autowired
	private AreaGroupService areaGroupService;
	
	
	/**
	 * 导向进存途销页面
	 * @param model
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "toJCTX")
	public String toFirstPage(Model model, ServletRequest request) {
		
		//查出当前用户关联的商家列表
		ShiroUser curUser = (ShiroUser) SecurityUtils.getSubject().getPrincipal();
		List<Customers> customerList=this.customersService.loadCustomerByAuthority(curUser);
		model.addAttribute("customerList",customerList);
		
		//如果是商家用户,初始化数据
		if("customer".equals(curUser.userType)){
			String customerCode=customerList.get(0).getCustomerCode();
			model.addAttribute("customerCode", customerCode);
			//初始化仓库数据
			List<AreaGroup> warehouseList=this.areaGroupService.loadAreaGroupByAuthority(curUser,customerCode);
			model.addAttribute("warehouseList", warehouseList);
		}
		return "report/jctx";
	}
	
	
	 /**
	  * 数据源—进
	  * @param params
	  * @param request
	  * @param response
	  * @throws Exception
	  */
	 @RequestMapping(value="/bindData_in", method=RequestMethod.POST) 
    public void bindData_in(@RequestBody JSONParam[] paramJson,HttpServletRequest request, HttpServletResponse response) throws Exception {
		try {
			//变量定义
			PrintWriter out=response.getWriter();
			JSONObject json=new JSONObject();
			Page<InstorageInfo> page=null;
			
			//当前用户
			Map<String, Object> param=new HashMap<String, Object>();
           ShiroUser shiroUser = (ShiroUser) SecurityUtils.getSubject().getPrincipal();
			
			//客户端参数
			Map<String, String> paramMap = DataTablesUtils.convertToMap(paramJson);
			String sEcho = paramMap.get("sEcho");  
			int start = Integer.parseInt(paramMap.get("iDisplayStart"));
			int length = Integer.parseInt(paramMap.get("iDisplayLength")); 
            String customerCode = paramMap.get("customerCode");
            String warehouseCode = paramMap.get("warehouseCode");
            String dataRange = paramMap.get("dataRange");
            String sku = paramMap.get("sku");
			
			//封装查询参数
			if(StringUtils.isEmpty(customerCode)){ //如果商家编码为空，赋予默认权限值
				String customerCodes=this.customersService.loadCustomerByAuthority(shiroUser.jobuuid);
				param.put("customerCodes", customerCodes);
			}else{
				param.put("customerCode", customerCode);
			}
			
			if(StringUtils.isEmpty(warehouseCode)){ //如果仓库编码为空，赋予默认权限值
				String areagroups=shiroUser.areagroups;
				param.put("areagroups", areagroups);
			}else{
				param.put("warehouseCode", warehouseCode);
			}
		
			if(StringUtils.isNotEmpty(dataRange)){
				 Map<String, Date> dataMap=DateUtils.getDateRange(dataRange);
	             Date fromDate=dataMap.get("fromDate");
	             Date toDate=dataMap.get("toDate");
	             param.put("fromDate", fromDate);
	             param.put("toDate", toDate);
			}
           
			param.put("sku", sku);
			
			//查询
//			int curPage=(start/length)+1;
//			page = this.ediOrderService.loadPageOrder(param,curPage, length);
//	        List<EdiOrder> eOrderList=page.getContent();
	        
	        //封装查询结果
//	        json.put("aaData", eOrderList);
	        json.put("sEcho", sEcho);
	        json.put("iTotalRecords", page.getTotalElements());
	        json.put("iTotalDisplayRecords", page.getTotalElements());
	        
	        out.print(json);
       
		} catch (Exception e) {
			
		}
   } 
	
}
